#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))

require "#{LKP_SRC}/lib/statistics"
require "#{LKP_SRC}/lib/log"
require "#{LKP_SRC}/lib/string_ext"

while (line = $stdin.gets)
  line = line.uncolorize
  next unless line =~ /[0-9]+:\s+([0-9.]+)\s+(bytes|B|KB|MB).*times -->\s+([0-9.]+) (Mbps|Gbps)\s+in\s+([0-9.]+) (nsec|usec|msec)/

  # 122:   8.389 MB      1000 times -->   66.191 Gbps  in    1.014 msecs

  if ($1.to_i < (8 * 1024) && ($2 == 'B' || $2 == 'bytes')) ||
     ($1.to_i < 8 && $2 == 'KB')
    case $6
    when 'nsec'
      usec = $5.to_f / 1000
    when 'usec'
      usec = $5.to_f
    when 'msec'
      usec = $5.to_f * 1000
    end

    less_8k_usec ||= []
    less_8k_usec << usec
  elsif ($1.to_i > (5 * 1024 * 1024) && ($2 == 'B' || $2 == 'bytes')) ||
        ($1.to_i > (5 * 1024) && $2 == 'KB') ||
        ($1.to_i > 5 && $2 == 'MB')
    bigger_5m_mbps ||= []
    bigger_5m_mbps << $3.to_f
  end
end

if less_8k_usec.nil? || bigger_5m_mbps.nil?
  log_error 'no less_8k_usec or bigger_5m_mbps in the STDIN'
  exit
end
puts "less_8K_usec.avg: #{less_8k_usec.average}"
puts "bigger_5M_Mbps.avg: #{bigger_5m_mbps.average}"
